﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Arm2015.WebUI.Infrastructure
{
    public static class SessionManager
    {
        private const string __CURRENT_USER_INFO = "CURRENT_USER_INFO";
        private const string __CURRENT_CONTRACTOR_INFO = "CURRENT_CONTRACTOR_INFO";
        private const string __SELECTED_CONTRACT_ID = "SELECTED_CONTRACT_ID";
        private const string __SELECTED_CONTRACTOR_ID = "SELECTED_CONTRACTOR_ID";
        private const string __JOURNAL_START_DATE = "JOURNAL_START_DATE";
        private const string __JOURNAL_END_DATE = "JOURNAL_END_DATE";
        private const string __CONTRACT_MAKERS = "CONTRACT_MAKERS";
        private const string __EDIT_JOURNAL_ROW = "EDIT_JOURNAL_ROW";

        private static HttpSessionState Session
        {
            get
            {
                return HttpContext.Current.Session;
            }
        }

        public static Models.UserInfo CurrentUserInfo
        {
            get
            {
                var sessionValue = Session[__CURRENT_USER_INFO];
                if (sessionValue == null)
                {
                    Models.UserInfo userInfo = null;
                    using (var context = new Storage.UsersEntities())
                    {
                        var login = "OOOZhasmin";

                        var user = context.D_USER.FirstOrDefault(u => u.LOGIN.ToLower() == login.ToLower());
                        userInfo = new Models.UserInfo
                        {
                            C_USER = user.C_USER,
                            LOGIN = user.LOGIN,
                            C_ROLE = user.C_ROLE,
                            DISPLAY_NAME = user.DISPLAY_NAME,
                            CONTRACTOR_UNIQUE_VALUE = user.D_USER_JOIN_SMETA
                            .Where(x => x.KIND == 0)
                            .FirstOrDefault().UNIQUE_VALUE
                        };
                    }

                    Session[__CURRENT_USER_INFO] = userInfo;
                }

                return (Models.UserInfo)Session[__CURRENT_USER_INFO];
            }
            set
            {
                Session[__CURRENT_USER_INFO] = value;
            }
        }

        public static Models.ContractorInfo CurrentContractorInfo
        {
            get
            {
                var sessionValue = Session[__CURRENT_CONTRACTOR_INFO];
                if (sessionValue == null)
                {
                    Models.ContractorInfo contractorInfo = null;
                    using (var context = new Storage.DataEntities())
                    {
                        var contractor = context.D_CONTRACTOR.FirstOrDefault(x => x.UNIQUE_VALUE == CurrentUserInfo.CONTRACTOR_UNIQUE_VALUE);

                        contractorInfo = new Models.ContractorInfo
                        {
                            C_CONTRACTOR = contractor.C_CONTRACTOR,
                            C_GENERAL_CONTRACTOR = contractor.D_GENERAL_CONTRACTOR.FirstOrDefault() == null ? null : (int?)contractor.D_GENERAL_CONTRACTOR.FirstOrDefault().C_GENERAL_CONTRACTOR,
                            CONTRACTS = new List<Models.ContractInfo>()
                        };


                        if (contractorInfo.IS_GENERAL)
                        {
                            contractorInfo.C_GENERAL_CONTRACT = context.D_GENERAL_CONTRACT.FirstOrDefault(x => x.C_GENERAL_CONTRACTOR == contractorInfo.C_GENERAL_CONTRACTOR).C_GENERAL_CONTRACT;

                            contractorInfo.CONTRACTS = context.D_CONTRACT
                                .Where(x => x.C_GENERAL_CONTRACT == contractorInfo.C_GENERAL_CONTRACT || x.C_CONTRACTOR == contractorInfo.C_CONTRACTOR)
                                .Select(x => new Models.ContractInfo
                                {
                                    C_CONTRACT = x.C_CONTRACT,
                                    N_BLOCK = x.N_BLOCK,
                                    C_CONTRACTOR = x.C_CONTRACTOR,
                                    N_CONTRACTOR = x.D_CONTRACTOR.N_CONTRACTOR,
                                    C_GENERAL_CONTRACT = x.C_GENERAL_CONTRACT,
                                    N_GENERAL_CONTRACT = x.D_GENERAL_CONTRACT.NUMBER,
                                    FIRST_SELECT = x.C_CONTRACTOR == contractorInfo.C_CONTRACTOR
                                }).ToList();
                        }
                        else
                        {
                            contractorInfo.CONTRACTS = context.D_CONTRACT.Where(x => x.C_CONTRACTOR == contractorInfo.C_CONTRACTOR)
                                .Select(x => new Models.ContractInfo
                                {
                                    C_CONTRACT = x.C_CONTRACT,
                                    N_BLOCK = x.N_BLOCK,
                                    C_CONTRACTOR = x.C_CONTRACTOR,
                                    N_CONTRACTOR = x.D_CONTRACTOR.N_CONTRACTOR,
                                    C_GENERAL_CONTRACT = x.C_GENERAL_CONTRACT,
                                    N_GENERAL_CONTRACT = x.D_GENERAL_CONTRACT.NUMBER
                                }).ToList();

                            if (contractorInfo.CONTRACTS.Count != 0)
                            {
                                contractorInfo.CONTRACTS[0].FIRST_SELECT = true;
                            }
                        }
                    }
                    Session[__CURRENT_CONTRACTOR_INFO] = contractorInfo;
                }

                return (Models.ContractorInfo)Session[__CURRENT_CONTRACTOR_INFO];
            }
            set
            {
                Session[__CURRENT_CONTRACTOR_INFO] = value;
            }
        }

        public static int SelectedContractId
        {
            get
            {
                var sessionValue = Session[__SELECTED_CONTRACT_ID];
                if (sessionValue == null)
                {
                    var contractId = CurrentContractorInfo.CONTRACTS.FirstOrDefault(x => x.FIRST_SELECT == true).C_CONTRACT;
                    Session[__SELECTED_CONTRACT_ID] = contractId;
                }

                return (int)Session[__SELECTED_CONTRACT_ID];
            }
            set
            {
                Session[__SELECTED_CONTRACT_ID] = value;
            }
        }

        public static int SelectedContractorId
        {
            get
            {
                return (int)Session[__SELECTED_CONTRACTOR_ID];
            }
            set
            {
                Session[__SELECTED_CONTRACTOR_ID] = value;
            }
        }

        public static DateTime JournalDateStart
        {
            get
            {
                var sessionValue = Session[__JOURNAL_START_DATE];
                if (sessionValue == null)
                {
                    //TODO:Заменить на текущую дату
                    var date = new DateTime(2014, 12, 27);
                    Session[__JOURNAL_START_DATE] = date;
                }

                return (DateTime)Session[__JOURNAL_START_DATE];
            }
            set
            {
                Session[__JOURNAL_START_DATE] = value;
            }
        }

        public static DateTime JournalDateEnd
        {
            get
            {
                var sessionValue = Session[__JOURNAL_END_DATE];
                if (sessionValue == null)
                {
                    var date = JournalDateStart.AddDays(1).AddSeconds(-1);
                    Session[__JOURNAL_END_DATE] = date;
                }

                return (DateTime)Session[__JOURNAL_END_DATE];
            }
            set
            {
                Session[__JOURNAL_END_DATE] = value;
            }
        }

        public static Models.Journal.Row EditJournalRow
        {
            get
            {
                var sessionValue = Session[__EDIT_JOURNAL_ROW];
                return sessionValue == null
                    ? new Models.Journal.Row()
                    : (Models.Journal.Row)sessionValue;
            }
            set
            {
                Session[__EDIT_JOURNAL_ROW] = value;
            }
        }
    }
}